﻿using System;
using System.IO;

using Bioinformatics.Algorithms;


namespace PsomlCommandLine
{
    class Program
    {
        static void Main(string[] args)
        {
            //check for arguments file
            if(args.Length == 0)
            {
                Console.WriteLine("Error: No runtime parameters passed.");
                //pause the console, prompt to quit
                Console.WriteLine("Press any key to continue . . .");
                Console.ReadKey();
                return;
            }

            //iterate through each argument file
            for (var i = 0; i < args.Length; i++)
            {
                //help avoid garbage collector releasing large objects half way through runs
                GC.Collect();
                GC.WaitForPendingFinalizers();

                var psomlAlgorithm = new Psoml();
                //load paramters from file - program quits if this part fails
                try
                {
                    psomlAlgorithm.SetParametersFromFile(Environment.CurrentDirectory + "\\" + args[i]);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error parsing arguments file: " + ex.Message);
                    Console.WriteLine("Press any key to continue . . .");
                    Console.ReadKey();
                    return;
                }
                //begin optimization process
                try
                {
                    psomlAlgorithm.Run();
                    psomlAlgorithm.OutputFileStream.Close();
                    psomlAlgorithm.OutputFileStream.Dispose();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error iterating population: " + ex.Message);
                    Console.WriteLine("Press any key to continue . . .");
                    Console.ReadKey();
                    return;
                }
            }
            Console.WriteLine("Program successfully finished.");
            Console.WriteLine("Press any key to continue . . .");
            Console.ReadKey();
        }
    }
}
